{% extends template('page', 'Combodo\\iTop\\Portal\\Brick\\BrowseBrick', oBrick) %}

{#
	Documentation :
	#brick_content_tree is populated by JS

	#brick_search_field works differently regarding the brick data loading mode :
		- When set to "full", all the tree is already populated and the search looks through it.
		- When set to "lazy", if the tree is partially loaded, the search will first load it completely, then only perform the search.
#}

{% block pPageScripts %}
	{{ parent() }}
	<script type="text/javascript" src="{{ app['combodo.portal.base.absolute_url'] }}lib/jquery-treelistfilter/js/TreeListFilter.js"></script>
{% endblock %}

{% block bBrowseMainContent %}
	<div id="brick_content_toolbar">
        <div class="ipb-button--group">
            <a href="#" class="ipb-button ipb-is-default" id="btn-collapse-all" title="{{ 'Brick:Portal:Browse:Tree:CollapseAll'|dict_s }}">
                    <span class="fa-layers">
                        <i class="fas fa-angle-down" data-fa-transform="up-4"></i>
                        <i class="fas fa-angle-up" data-fa-transform="down-4"></i>
                    </span>
            </a>
            <a href="#" class="ipb-button ipb-is-default" id="btn-expand-all" title="{{ 'Brick:Portal:Browse:Tree:ExpandAll'|dict_s }}">
                <span class="fa-layers">
                        <i class="fas fa-angle-down" data-fa-transform="down-4"></i>
                        <i class="fas fa-angle-up" data-fa-transform="up-4"></i>
                    </span>
            </a>
		</div>
		<div>
			<label>{{ 'Portal:Datatables:Language:Search'|dict_s }}<input type="search" class="form-control input-sm" id="brick_search_field" placeholder="" aria-controls="brick_main_table" value="{{ sSearchValue }}"></label>
		</div>
	</div>
	<ul class="list-group tree" id="brick_content_tree" data-level-id="L">
	</ul>

	<div id="brick_content_empty" class="text-center">
		{{ 'Brick:Portal:Browse:Filter:NoData'|dict_s }}
	</div>
	<div id="brick_tree_overlay">
		<div class="overlay_content">
            {% include template('loader') %}
		</div>
	</div>
{% endblock %}

{% block pPageLiveScripts %}
	{{ parent() }}

	<script type="text/javascript">
		var sNodeCollapsedClass = 'glyphicon-menu-right';
		var sNodeExpandedClass = 'glyphicon-menu-down';
		var sNodeLoadingClass = 'glyphicon-refresh keep-spinning';
		var iSearchDelay = 500;
		var sBrowseMode = '{{ sBrowseMode }}';
		var oLevelsProperties = {{ aLevelsProperties|raw }};
		var oRawDatas = {{ aItems|raw }};
		var bIsFullyLoaded = ('{{ sDataLoading }}' === '{{ constant('Combodo\\iTop\\Portal\\Brick\\AbstractBrick::ENUM_DATA_LOADING_FULL') }}') ? true : false;

		// Collapses / Expands all the tree nodes
		var collapseAll = function()
		{
			$('#brick_content_tree .tree').toggle(false);
			$('#brick_content_tree .tree-toggle .glyphicon').removeClass(sNodeExpandedClass+' '+sNodeLoadingClass).addClass(sNodeCollapsedClass);
		};
		var expandAll = function()
		{
			$('#brick_content_tree .tree').toggle(true);
			$('#brick_content_tree .tree-toggle .glyphicon').removeClass(sNodeCollapsedClass+' '+sNodeLoadingClass).addClass(sNodeExpandedClass);
		};
		// Show a loader over the tree
		var showTreeLoader = function()
		{
			$("#brick_content_tree").hide();
			$('#brick_tree_overlay').show();
		};
		// Hide the loader over the tree
		var hideTreeLoader = function()
		{
			$('#brick_tree_overlay').hide();
			$("#brick_content_tree").show();
		};
		// Registers the toggle listeners on the tree nodes. Used after every AJAX calls.
		var registerToggleListeners = function()
		{
			$('#brick_content_tree .tree-toggle').off('click').on('click', function (oEvent) {
				oEvent.preventDefault();

				// Forcing subitems to expand after a filter, so we can browse subitems of a filtered item. Else is the regular toggle
				if($(this).parent().children('ul.tree:visible').length > 0 && $(this).parent().children('ul.tree:visible').children('li:visible').length === 0)
				{
					$(this).parent().children('ul.tree').children('li').toggle(true);
				}else{
					$(this).parent().children('ul.tree').toggle(200);
				}

				// Toggling glyphicon class
				if($(this).find('.glyphicon').hasClass(sNodeCollapsedClass))
				{
					$(this).find('.glyphicon').removeClass(sNodeCollapsedClass+' '+sNodeLoadingClass).addClass(sNodeExpandedClass);
				}
				else
				{
					$(this).find('.glyphicon').removeClass(sNodeExpandedClass+' '+sNodeLoadingClass).addClass(sNodeCollapsedClass);
				}

				// Check if the node has no children, if so we try to load them through AJAX (Only for the current item)
				if($(this).parent().children('ul.tree').children('li').length === 0)
				{
					$(this).find('.glyphicon').removeClass(sNodeCollapsedClass+' '+sNodeExpandedClass).addClass(sNodeLoadingClass);
					loadChildNodes($(this).attr('data-level-alias'), $(this).attr('data-item-id'));
				}
			});

            // Allows link in item's description to work. Otherwise, the predentDefault of the item takes over.
            $('#brick_content_tree .list-group-item-description a').off('click').on('click', function(oEvent){
                oEvent.stopPropagation();
            });
		};
		// Registers the filter listeners on the tree.
		var registerFilterListeners = function()
		{
			$('#brick_search_field').treeListFilter('#brick_content_tree', iSearchDelay, filterResultsHandler);
		};
		var filterResultsHandler = function()
		{
			// If results shows intermediate levels without any leaves under, we show all its children.
			$('#brick_content_tree .list-group-item:visible').each(function(iIndex, oElement){
				if($(oElement).find('.list-group.tree:visible').length === 0)
				{
					$(oElement).find('.list-group.tree:not(:visible)').show();
					$(oElement).find('.list-group.tree:not(:visible) .list-group-item').show();
				}
				else if($(oElement).find('.list-group.tree:visible .list-group-item:visible').length === 0)
				{
					$(oElement).find('.list-group.tree:visible .list-group-item:not(:visible)').show();
				}
			});

			// Show / hide empty data message
			if(bIsFullyLoaded)
			{
				hideTreeLoader();

				if($('#brick_content_tree > .list-group-item:visible').length > 0)
				{
					$('#brick_content_empty').hide();
				}
				else
				{
					$('#brick_content_empty').show();
				}
			}

            // hide/show tree items filter data when search input is empty/notEmpty
            $('.tree-item-filter-data').toggle($('#brick_search_field').val() !== '');

			expandAll();
		};
		// Load current node childnodes throught AJAX
		var loadChildNodes = function(sLevelAlias, sNodeId)
		{
			var sUrl = '{{ app.url_generator.generate('p_browse_brick_mode_tree', {'sBrickId': sBrickId, 'sBrowseMode': sBrowseMode, 'sLevelAlias': '-sLevelAlias-', 'sNodeId': '-sNodeId-'})|raw }}';
			sUrl = sUrl.replace(/-sLevelAlias-/, sLevelAlias).replace(/-sNodeId-/, sNodeId);

			$.ajax(sUrl)
			.done(function(data) {
				$('#brick_content_tree .tree-toggle[data-level-alias="'+sLevelAlias+'"][data-item-id="'+sNodeId+'"] .glyphicon').removeClass(sNodeCollapsedClass+' '+sNodeLoadingClass).addClass(sNodeExpandedClass);
				for(index in data.data)
				{
					var sublevel = data.data[index];
					var sublevelData = {};
					sublevelData[sublevel.level_alias+"::"+sublevel.id] = sublevel;
					buildTree(sublevelData, sLevelAlias+"::"+sNodeId, false);
				}
				registerToggleListeners();
			})
			.fail(function() {
				alert('{{ 'Error:XHR:Fail'|dict_format(constant('ITOP_APPLICATION_SHORT')) }}');
			});
		};
		// Build tree nodes from data under the nodeId
		var buildTree = function(data, nodeId, isRootLevel)
		{
			if(nodeId === undefined)
			{
				// We are on the root node
				nodeId = 'L';
				$('ul[data-level-id="'+nodeId+'"]').html('');
			}
			if(isRootLevel === undefined)
			{
				isRootLevel = true;
			}

			$.each(data, function(i, item){
				var levelId = item.level_alias+'::'+item.id;
				var levelAltId = item.level_alias+'_'+item.id;
				var levelActions = oLevelsProperties[item.level_alias].actions;
				var levelActionsKeys = Object.keys(levelActions);
				var levelPrimaryAction = levelActions[levelActionsKeys[0]];
				var url = '';

                var liElem = $('<li></li>').addClass('list-group-item');
                var spanElem = $('<span></span>').addClass('tree-item-wrapper').attr('data-item-id', item.id).attr('data-level-alias', item.level_alias);
                spanElem.attr('data-tree-additional-search', item['filter_data']['values']);
                let iconElem = $('<div>').addClass('tree-item-image');
                if( (item.image !== undefined) && (item.image !== '') && (item.image !== null) )
                {
                    iconElem.append( $('<img />').attr('src', item.image));
                }
                else if ((item.name !== undefined) && (item.name !== '')){
                    iconElem.append('<span>'+item.name.charAt(0)+'</span>');
                }
                var nameElem = $('<a></a>').addClass('tree-item').text(item.name);
                // Building node
                $('ul[data-level-id="'+nodeId+'"]').append(liElem);
                spanElem.append(iconElem);
                spanElem.append(nameElem);
                liElem.append(spanElem);

                // Delegating a click on <span> to its child <a> element
                // N°5396 - Fix BrowseBrick tree "opening_target" mode for "self" and "new" values
                spanElem.on('click', function (event) {
                    var aElem = $(this).children('a.tree-item')[0];
                    if (aElem !== event.target) {
                        aElem.trigger('click');
                    }
                });



				// Building actions for that node
				switch(levelPrimaryAction.type)
				{
					case '{{ constant('Combodo\\iTop\\Portal\\Brick\\BrowseBrick::ENUM_ACTION_DRILLDOWN') }}':
                        spanElem.addClass('tree-toggle');
                        nameElem.html('<span class="glyphicon '+sNodeCollapsedClass+'" aria-hidden="true"></span><span class="list-group-item-text">'+item.name+'</span>');
                        break;
					case '{{ constant('Combodo\\iTop\\Portal\\Brick\\BrowseBrick::ENUM_ACTION_VIEW') }}':
						url = '{{ app.url_generator.generate('p_object_view', {'sObjectClass': '-objectClass-', 'sObjectId': '-objectId-'})|raw }}'.replace(/-objectClass-/, item.class).replace(/-objectId-/, item.id);
						SetActionUrl(nameElem, url);
                        SetActionOpeningTarget(nameElem, levelPrimaryAction.opening_target);
                        break;
					case '{{ constant('Combodo\\iTop\\Portal\\Brick\\BrowseBrick::ENUM_ACTION_EDIT') }}':
						url = '{{ app.url_generator.generate('p_object_edit', {'sObjectClass': '-objectClass-', 'sObjectId': '-objectId-'})|raw }}'.replace(/-objectClass-/, item.class).replace(/-objectId-/, item.id);
						SetActionUrl(nameElem, url);
                        SetActionOpeningTarget(nameElem, levelPrimaryAction.opening_target);
                        break;
					case '{{ constant('Combodo\\iTop\\Portal\\Brick\\BrowseBrick::ENUM_ACTION_CREATE_FROM_THIS') }}':
						url = levelPrimaryAction.url.replace(/-objectClass-/, item.class).replace(/-objectId-/, item.id);
						url = CombodoGlobalToolbox.AddParameterToUrl(url, 'ar_token', item.action_rules_token[levelPrimaryAction.type]);
						SetActionUrl(nameElem, url);
                        SetActionOpeningTarget(nameElem, levelPrimaryAction.opening_target);
						break;
					default:
						//console.log('Action "'+levelPrimaryAction.type+'" not implemented for primary action');
						break;
				}

                // Add filter data if present
                if(item['filter_data']['values'] !== '')
                {
                    nameElem.append('<div class="tree-item-filter-data">' + item['filter_data']['values_and_codes'] + '</div>');
                }


                // Building tooltip for the node
                // N°4662 - Surround tooltip with div to ensure text retrival
                if ((item.tooltip !== undefined) && ($('<div>'+item.tooltip+'</div>').text() !== ''))
                {
                    nameElem.attr('data-tooltip-content', item.tooltip).attr('data-tooltip-html-enabled', true);
                    CombodoTooltip.InitTooltipFromMarkup(nameElem);
                }
                // Building description for the node
                if ((item.description !== undefined) && (item.description !== ''))
                {
                    nameElem.append($('<span class="list-group-item-description">'+item.description+'</span>'));
                }
				
				if(levelActionsKeys.length > 1)
				{
					// Retrieving secondary action (Now we also display primary action)
					var actionsButtons = {};
					for(j = 0; j < levelActionsKeys.length; j++)
					{
						actionsButtons[levelActionsKeys[j]] = levelActions[levelActionsKeys[j]];
					}

					// Preparing secondary actions container
					var actionsElem = $('<div></div>').addClass('list-group-item-actions');
					liElem.append(actionsElem);
					// Checking if a menu is necessary
					var actionsSSTogglerElem = $('<a class="ipb-button ipb-is-alternative ipb-is-default fas fa-ellipsis-v" data-toggle="ipb-dropdown" data-target="#item-actions-menu-'+levelAltId+'"></a>');
					var actionsSSMenuElem = $('<ipb-dropdown id="item-actions-menu-'+levelAltId+'" class="item-action-wrapper" data-placement="left-bottom"></ipb-dropdown>');
					actionsElem.append(actionsSSTogglerElem);
					actionsElem.append(actionsSSMenuElem);

					// Adding secondary actions
					for(j in actionsButtons)
					{
						var action = actionsButtons[j];
						var actionElem = $('<a></a>');
						var actionIconElem = $('<span></span>').addClass('ipb-dropdown--icon').appendTo(actionElem);

						switch(action.type)
						{
							case '{{ constant('Combodo\\iTop\\Portal\\Brick\\BrowseBrick::ENUM_ACTION_VIEW') }}':
								url = '{{ app.url_generator.generate('p_object_view', {'sObjectClass': '-objectClass-', 'sObjectId': '-objectId-'})|raw }}'.replace(/-objectClass-/, item.class).replace(/-objectId-/, item.id);
								break;
							case '{{ constant('Combodo\\iTop\\Portal\\Brick\\BrowseBrick::ENUM_ACTION_EDIT') }}':
								url = '{{ app.url_generator.generate('p_object_edit', {'sObjectClass': '-objectClass-', 'sObjectId': '-objectId-'})|raw }}'.replace(/-objectClass-/, item.class).replace(/-objectId-/, item.id);
								break;
							case '{{ constant('Combodo\\iTop\\Portal\\Brick\\BrowseBrick::ENUM_ACTION_CREATE_FROM_THIS') }}':
								url = action.url.replace(/-objectClass-/, item.class).replace(/-objectId-/, item.id);
								url = CombodoGlobalToolbox.AddParameterToUrl(url, 'ar_token', item.action_rules_token[action.type]);
								break;
							default:
							    url = '#';
								//console.log('Action "'+action.type+'" not implemented for secondary action');
								break;
						}
                        SetActionUrl(actionElem, url);
                        SetActionOpeningTarget(actionElem, action.opening_target);

                        // Adding title if present
						if(action.title !== undefined)
						{
							actionElem.attr('title', action.title);
						}
						// Adding icon class if present
						if(action.icon_class !== undefined)
						{
							actionIconElem.addClass(action.icon_class);
						}

						actionElem.append(action.title);
                        actionsSSMenuElem.append( $('<li></li>').append(actionElem) );
					}
				}

				// Building subnodes if necessary
				var ulElem  = $('<ul></ul>').addClass('list-group').addClass('tree').attr('data-level-id', levelId);
				liElem.append(ulElem);
				if(item.subitems.length !== 0)
				{
					buildTree(item.subitems, levelId, false);
				}
			});

			// Update listeners
			if(isRootLevel)
			{
				registerToggleListeners();
			}
		};

		// N°3995: Loader is shown immediately, otherwise when we have a huge amount of items, we can have a bottleneck on the buildTree() function, blocking the display of the loader
		showTreeLoader();
		$(document).ready(function(){
			// Init expand/collapse all buttons
			$('#btn-collapse-all').on('click', function (oEvent) {
			    oEvent.preventDefault();
				collapseAll();
			});
			$('#btn-expand-all').on('click', function (oEvent) {
                oEvent.preventDefault();
                if(!bIsFullyLoaded)
				{
					// Show a loader while fetching results
					showTreeLoader();

					// If we don't do that now, we have have several calls
					bIsFullyLoaded = true;

					// Display loader by toggling glyphicon class
					$('#brick_content_tree .tree-toggle .glyphicon.'+sNodeCollapsedClass).removeClass(sNodeCollapsedClass).addClass(sNodeLoadingClass);

					// Load the whole tree
					$.ajax('{{ app.url_generator.generate('p_browse_brick_mode', {'sBrickId': sBrickId, 'sBrowseMode': sBrowseMode, 'sDataLoading': constant('Combodo\\iTop\\Portal\\Brick\\AbstractBrick::ENUM_DATA_LOADING_FULL')})|raw }}')
					.done(function(data)
					{
						buildTree(data.data);
					})
					.fail(function(){
						bIsFullyLoaded = false;
					})
					.always(function(){
						$('#brick_content_tree .tree-toggle .glyphicon').removeClass(sNodeCollapsedClass+' '+sNodeLoadingClass).addClass(sNodeExpandedClass);
						// Hide loader no matter what
						hideTreeLoader();
					});
				}
				else
				{
					$('#brick_content_tree .tree-toggle .glyphicon').removeClass(sNodeCollapsedClass+' '+sNodeLoadingClass).addClass(sNodeExpandedClass);
				}
				expandAll();
			});

			// Init filter field
			// Note : If placed in the registerFilterListeners function, must be before the .treeListFilter as the off('change') will remove .treeListFilter
			$('#brick_search_field').on('change', function(oEvent){
				// Show a loader while fetching/filtering results
				showTreeLoader();

				if(!bIsFullyLoaded)
				{

					// We don't want to trigger TreeListFilter yet
					oEvent.stopPropagation();

					// Load the whole tree
					$.ajax('{{ app.url_generator.generate('p_browse_brick_mode', {'sBrickId': sBrickId, 'sBrowseMode': sBrowseMode, 'sDataLoading': constant('Combodo\\iTop\\Portal\\Brick\\AbstractBrick::ENUM_DATA_LOADING_FULL')})|raw }}')
					.done(function(data)
					{
						bIsFullyLoaded = true;
						// Updating tree
						buildTree(data.data);
						// Trigerring filter
						$('#brick_search_field').trigger('change');
					})
					.fail(function(){
						bIsFullyLoaded = false;
					})
					.always(function(){
						// We don't need to call this because it will be called as a callback when "change" event is triggered on treeListFilter
						//filterResultsHandler();
					});
				}
				else
				{
					// // We don't need to call this because it will be called as a callback when "change" event is triggered on treeListFilter
					filterResultsHandler();
				}
			});

			// Auto collapse item actions popup
			$('body').on('click', function(){
				$('#brick_content_tree .item-action-wrapper.collapse.in').collapse('hide');
			});

			// Build the tree (collapsed)
			buildTree(oRawDatas);
			hideTreeLoader();
			registerFilterListeners();
			collapseAll();

            // Open first level if only one item and not pre-filtering
            if( ($('#brick_content_tree > .list-group-item').length == 1) && ($('#brick_search_field').val() === '') )
            {
                setTimeout(function(){
                    $('#brick_content_tree > .list-group-item > .tree-toggle').trigger('click');
                }, 300);
            }

			{% if (sSearchValue is not null) and (sSearchValue != '') %}
				// Filters from default value
				$('#brick_search_field').trigger('change');
			{% endif %}
		});
	</script>
{% endblock %}